package com.bodu.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.exception.custom.DeleteException;
import com.bodu.core.exception.custom.SaveException;
import com.bodu.core.exception.custom.SearchException;
import com.bodu.core.exception.custom.UpdateException;
import com.bodu.system.entity.po.ProjectPo;
import com.bodu.system.entity.request.ProjectRequest;
import com.bodu.system.entity.vo.ProjectVo;
import com.bodu.system.mapper.ProjectMapper;
import com.bodu.system.service.ProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public ProjectVo getModel(Long id) throws SearchException {
        ProjectPo projectPo =  projectMapper.getModel(id);
        ProjectVo projectVo = new ProjectVo();
        BeanUtil.copyProperties(projectPo, projectVo);
        return projectVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long save(ProjectRequest request) throws SaveException {
        projectMapper.save(request);
        return request.getId();
    }

    @Override
    public Long update(ProjectRequest request) throws UpdateException {
        projectMapper.update(request);
        return request.getId();
    }

    @Override
    public List<ProjectVo> listPage(ParamReq<ProjectRequest> params) throws SearchException {
        return projectMapper.listPage(params);
    }

    @Override
    public Integer countPage(ParamReq<ProjectRequest> params) throws SearchException {
        return projectMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) throws DeleteException {
        return projectMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public ProjectVo getOneModel(ProjectRequest request) {
        ProjectPo projectPo = projectMapper.getModelLimit1(request.getProjectType());
        ProjectVo vo = new ProjectVo();
        BeanUtil.copyProperties(projectPo, vo);
        return vo;
    }
}
